﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;
using HRProject.Code.Common;

namespace HRProject.Code.DAO
{
    public class WorktimeDAO : BaseDAO
    {
        public WorktimeDAO()
            : base ()
        {
        }

        public BLWorktimeSheet Collect(BLJobTitle jobTitleFilter, Guid agentId, string fullName, int year, int month)
        {
            BLWorktimeSheet result = new BLWorktimeSheet(year, month);
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@jobTitleId", jobTitleFilter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", agentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentFullName", fullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@filterDateDown", new DateTime(year, month, 1), ParameterDirection.Input));
            condition.Add(new BaseCondition("@filterDateUp", new DateTime(year, month, DateTime.DaysInMonth(year, month),23,59,59), ParameterDirection.Input));
            DataTable data = select("usp_CollectWorktimeSheet", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                Guid rAgentId = (Guid)data.Rows[i]["AgentId"];
                string rFullName = (string)data.Rows[i]["FullName"];
                DateTime startDate = (DateTime)data.Rows[i]["StartDate"];
                int duration = (int)data.Rows[i]["TotalDuration"];
                bool isApproved = (bool)data.Rows[i]["IsApproved"];
                result.SetItem(rAgentId, rFullName, startDate.Day, duration, isApproved);
            }
            return result;
        }

        public int insert(BLWorktime worktime)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", Guid.NewGuid(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", worktime.AgentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startTime", worktime.StartTime, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endTime", worktime.EndTime, ParameterDirection.Input));
            //condition.Add(new BaseCondition("@description", record.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature",worktime.DataSignature,ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode",errorCode,ParameterDirection.Output));
            int result = update("usp_AddWorktime", condition);
            return result;
        }

        public int update(BLWorktime worktime)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", worktime.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startTime", worktime.StartTime, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endTime", worktime.EndTime, ParameterDirection.Input));
            //condition.Add(new BaseCondition("@description", record.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", worktime.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateWorktime", condition);
            return result;
        }

        public int SetDefaultWorktime(Guid jobTitleId, int year, int month, ref int errorCode)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            errorCode = 0;
            condition.Add(new BaseCondition("@jobtitleId", jobTitleId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@year", year, ParameterDirection.Input));
            condition.Add(new BaseCondition("@month", month, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_SetDefaultWorktime", condition);
            errorCode = (int)condition[condition.Count - 1].ParamValue;
            return result;
        }

        public int delete(BLWorktime worktime)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@id", worktime.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", BLAuthentication.CurrentUserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", worktime.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteWorktime", condition);
            return result;
        }
    }
}
